#!/usr/bin/python
# -*- coding:utf-8 -*-

from urllib2 import *
from urllib import urlencode
from urlparse import urlparse
import cookielib

# modify username and password before run
username = ""
password = ""

proxy = "proxyhk.zte.com.cn:80"

testurl = "http://www.sina.com.cn"
headers = {'User-agent':'Mozilla/5.0'}


cj = cookielib.CookieJar()
proxy_handler = ProxyHandler({'http': proxy})
openeragent = build_opener(proxy_handler, HTTPCookieProcessor(cj))
openeragent.addheaders = [('User-agent', 'Mozilla/5.0')]

r = openeragent.open(testurl)
#print(r.read())

print(r.info())
up = urlparse(r.geturl())
loginurl = "http://" + up.netloc + "/ac_portal/login.php"
#opr=pwdLogin&userName=***&pwd=***&rememberPwd=0&lang=chs
data = {"opr":"pwdLogin", 
	"userName":username,
	"pwd":password,
	"rememberPwd":"0",
	"lang":"chs"}
print(loginurl)
print(urlencode(data))

try:
	r2 = openeragent.open("http://" + up.netloc + "/favicon.ico")
except Exception as e:
	pass


req = Request(loginurl, urlencode(data), headers, r.geturl()) 

print(cj)

try:
	r3 = openeragent.open(req)
except Exception as e:
	pass

print(r.read())
print(r.info())
print(cj)
print(r3.info())

